package cz.muni.fi.pv168.addressbook.entities;

import java.util.Objects;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author Michal Ďuriš
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class Contact {

    @XmlAttribute(required = true)
    private Long id;

    @XmlTransient
    private Person person = new Person();

    @XmlElement(required = true)
    private ContactType contactType;

    @XmlElement(required = true)
    private String contactValue;

    public Contact() {
    }

    public Contact(Person person, ContactType contactType, String contactValue) {

        this.person = person;
        this.contactType = contactType;
        this.contactValue = contactValue;
    }

    public void setContact(Contact contact) {

        id = contact.getId();
        person = contact.getPerson();
        contactType = contact.getContactType();
        contactValue = contact.getContactValue();

    }

    public ContactType getContactType() {
        return contactType;
    }

    public void setContactType(ContactType contactType) {
        this.contactType = contactType;
    }

    public String getContactValue() {
        return contactValue;
    }

    public void setContactValue(String contactValue) {
        this.contactValue = contactValue;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @XmlAttribute(name = "personID")
    public Long getPersonID() {
        return person.getId();
    }

    public void setPersonID(Long id) {
        person.setId(id);
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    @Override
    public String toString() {

        return contactValue;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Contact other = (Contact) obj;

        return Objects.equals(this.id, other.id);
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 71 * hash + Objects.hashCode(this.id);
        return hash;
    }

}
